package com.itheima.controller;

import cn.hutool.core.lang.UUID;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.log.LogAnno;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.QueryPageBean;
import com.itheima.pojo.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.constant.MessageConstant;
import com.itheima.pojo.*;
import com.itheima.service.SetmealService;
import com.itheima.utils.JedisUtil;
import com.itheima.utils.QiNiuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;
    @Autowired
    private JedisUtil jedisUtil;


    @LogAnno("查询所有检查组")
    @PreAuthorize("hasAnyAuthority('CHECKGROUP_QUERY')")
    @RequestMapping("/findAllCheckGroup")
    public Result findAllCheckGroup() {
        try {
            return Result.success("查询所有检查组成功", setmealService.findAllCheckGroup());
        } catch (Exception e) {
            return Result.error("查询所有检查组失败");
        }
    }

    @LogAnno("上传图片")
    @PreAuthorize("hasAnyAuthority('SETMEAL_ADD')")
    @RequestMapping("/upload")
    public Result upload(MultipartFile imgFile) {
        String originalFilename = imgFile.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        String name = UUID.randomUUID() + originalFilename.substring(index);
        try {
            QiNiuUtil.upload(imgFile.getBytes(), name);
            jedisUtil.sadd("setmealPicResources", name);
            return Result.success("上传图片成功", name);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error("上传图片失败");
        }
    }

    @LogAnno("添加套餐")
    @PreAuthorize("hasAnyAuthority('SETMEAL_ADD')")
    @RequestMapping("/addSetmeal")
    public Result addSetmeal(@RequestBody Setmeal setmeal) {
        try {
            setmealService.addSetmeal(setmeal);
            jedisUtil.sadd("setmealPicDbResources", setmeal.getImg());
            return Result.success("添加套餐成功");
        } catch (Exception e) {
            return Result.error("添加套餐失败");
        }
    }

    @LogAnno("查询分页套餐")
    @PreAuthorize("hasAnyAuthority('SETMEAL_QUERY')")
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return setmealService.findPage(queryPageBean);
    }

    @RequestMapping("/findById")
    public Result findById(int id) {
        try {
            Setmeal setmeal = setmealService.findDetailById(id);
            return new Result(true, "回显套餐成功", setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "回显套餐数据失败");
        }
    }

    /**
     * 根据id查询套餐
     *
     * @param
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {
            setmealService.update(setmeal, checkgroupIds);
        } catch (Exception e) {
            //更新套餐失败
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
    }


    /**
     * 根据套餐id删除套餐
     *
     * @param id
     * @return
     */
    @PostMapping("/deleteById")
    public Result deleteById(int id) {
        try {
            setmealService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            //删除套餐失败
            return new Result(false, MessageConstant.DELETE_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
    }

    /**
     * 根据套餐id查询检查组id集合
     *
     * @param id
     * @return
     */
    @PostMapping("/getCheckGroupIdByPkgId")
    public Result getCheckGroupIdByPkgId(int id) {
        List<Integer> checkGroupIdList = setmealService.getCheckGroupIdByPkgId(id);
        if (checkGroupIdList != null) {
            //查询成功
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupIdList);
        }
        return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
    }
}
